package com.zuoyy.admin.system.controller;

import com.zuoyy.admin.common.BaseController;
import com.zuoyy.admin.system.dto.ActionLogDTO;
import com.zuoyy.common.constant.DictSystemConst;
import com.zuoyy.common.vo.JsonResponse;
import com.zuoyy.modules.common.utils.DictUtil;
import com.zuoyy.modules.system.domain.ActionLog;
import com.zuoyy.modules.system.query.ActionLogQuery;
import com.zuoyy.modules.system.service.ActionLogService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zuo
 */
@Controller
@RequestMapping("/system/actionLog")
public class ActionLogController extends BaseController {

    //页面地址
    private static final String PAGE_PATH = "system/actionLog";
    //权限标示
    private static final String PERMISSION_FLAG = "system:actionLog:";

    @Autowired
    private ActionLogService actionLogService;

    @GetMapping("/index.shtml")
    @RequiresPermissions(PERMISSION_FLAG+"index")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("res", getMyResources(request,response));
        return PAGE_PATH+"/index";
    }

    @GetMapping("/list/page")
    @RequiresPermissions(PERMISSION_FLAG+"list")
    public String listPage(Model model) {
        model.addAttribute("button", getPageButton(PERMISSION_FLAG+"list"));
        return PAGE_PATH+"/list";
    }


    @ResponseBody
    @PostMapping("/list")
    public JsonResponse getListPage(ActionLogQuery query) {
        List<ActionLogDTO> list = new ArrayList<>();
        Page<ActionLog> page = actionLogService.getPageList(query);
        List<ActionLog> actionLogs = page.getContent();
        if(CollectionUtils.isNotEmpty(actionLogs)){
            for(ActionLog log : actionLogs){
                ActionLogDTO dto = new ActionLogDTO();
                try {
                    BeanUtils.copyProperties(dto,log);
                    dto.setLogType(DictUtil.keyValue(DictSystemConst.LOG_TYPE,log.getType().toString()));
                    list.add(dto);
                } catch (Exception e) {
                }

            }
        }
        return JsonResponse.success(setQueryResult(query.getDraw(), (int) page.getTotalElements(),list));
    }

    @ResponseBody
    @RequestMapping("/status/delete")
    @RequiresPermissions(PERMISSION_FLAG+"delete")
    public JsonResponse delete(
            @RequestParam(value = "ids", required = false) String id){
        if (!StringUtils.isEmpty(id)){
            actionLogService.deleteById(id);
        }else {
            actionLogService.deleteAll();

        }
        return JsonResponse.success();
    }

    @GetMapping("/detail/page/{id}")
    @RequiresPermissions(PERMISSION_FLAG+"detail")
    public String toDetail(@PathVariable("id") ActionLog actionLog, Model model){
        model.addAttribute("actionLog",actionLog);
        return PAGE_PATH+"/detail";
    }


}